
const Config = require("../config");
const WsConfig = Config.WebSocket
let WsStatus = WsConfig.status
let WsPort = WsConfig.port
const WebSocket = require('ws');
const ConversationModule = require("../api/conversation")
// Ws连接数
const linkWsNumber = 0

if (!WsStatus) {

} else {

    console.log("启动 WebSocket");

    // 创建WebSocket服务器实例
    const wss = new WebSocket.Server({ port: WsPort });

    // 服务开启事件
    wss.on('open', () => {
        console.log('websocket 服务已开启，端口：',WsPort)
    })
    wss.on('connection', (ws) => {

        console.log("ws", ws);

        // const WsRequestHeaders = ws.upgradeReq.headers;

        linkWsNumber++

        // 客户端关闭事件
        ws.on("close", async (Client) => {
            console.log("客户端关闭事件", Client);
        })
        // 客户端发消息
        ws.on('message', async (message) => {
            let JsonMessage = JSON.parse(message.toString())
            let MessageTypeOf = typeof JsonMessage
            if (MessageTypeOf != "object") {
                ws.send(JSON.stringify({
                    code: 0, msg: "收到您的消息,未知的会话类型"
                }))
            }
            let { type, action } = JsonMessage
            console.log({
                type, action
            });

            if (type == "Conversation") {
                // 获取会话列表
                if (action == "GetConversationList") {
                    console.log("执行 GetConversationList");
                }
                // 发起会话
                if (action == "StartConversation") {
                    console.log("执行 StartConversation");
                }
                // 阅读会话
                if (action == "LookConversation") {
                    console.log("执行 LookConversation");
                }
                // 删除会话
                if (action == "DeleteConversation") {
                    console.log("执行 DeleteConversation");
                }
                // 会话设置置顶
                if (action == "SetConversationTop") {
                    console.log("执行 SetConversationTop");
                }
                // 发送会话
                if (action == "PushConversationMessage") {
                    console.log("执行 PushConversationMessage");
                    let { code, msg } = await ConversationModule.PushConversationMessage(JsonMessage.data)
                    if (code == 0) {
                        ws.send(JSON.stringify({
                            type: "Conversation", action: "ConversationMessage", data: {
                                code,
                                msg,
                                data: JsonMessage.data
                            }
                        }));
                    } else {
                        ws.send(JSON.stringify({
                            type: "Conversation", action: "ConversationMessage", data: {
                                code,
                                msg
                            }
                        }));
                    }

                }
                ws.send(JSON.stringify({ name: "我" }));
            }
        });
    });
    // 服务关闭事件
    wss.on('close', function close() {
        console.log('websocket 服务已关闭')
    })
}